package com.kc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kc.ExceptionHandler.UndowException;
import com.kc.bean.AccountLikeArticle;
import com.kc.bean.Article;
import com.kc.bean.UserAccount;
import com.kc.mapper.AccountLikeArticleMapper;
import com.kc.service.IAccountLikeArticleService;
import com.kc.service.IArticleService;
import com.kc.service.IUserAccountService;
import com.kc.util.RedisUtil;
import com.kc.util.ResponesBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-10-26
 */
@Service
public class AccountLikeArticleServiceImpl extends ServiceImpl<AccountLikeArticleMapper, AccountLikeArticle> implements IAccountLikeArticleService {

    @Resource
    private AccountLikeArticleMapper accountLikeArticleMapper;

    @Resource
    private IArticleService articleService;

    @Resource
    private ResponesBean responesBean;

    @Resource
    private IUserAccountService userAccountService;

    @Override
    public ResponesBean LikeArticle(Article article, String token) {
        try {
            //获取userid
            UserAccount account = new UserAccount();

            account.setUserId(userAccountService.getAccount(token));

            article.setReadingNumber(article.getReadingNumber()+1);

            int i = articleService.updateArticle(article);
            if (i==1){
                AccountLikeArticle accountLikeArticle = new AccountLikeArticle();

                accountLikeArticle.setAccountId(account.getUserId());
                accountLikeArticle.setArticleId(article.getArtcileId());

                    int j = accountLikeArticleMapper.insert(accountLikeArticle);
                if (j==1){
                    responesBean.setAll(200,"点赞成功",null);
                    RedisUtil.Addscore(1,article.getArtcileId());
                    return responesBean;
                }
            }
        }catch (NullPointerException e){
            throw new UndowException("文章出错请重试"+e.getMessage());
        }
        responesBean.setAll(400,"点赞失败请登陆",null);
        return responesBean;
    }
}
